<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-tabs v-model="activeName">
          <el-tab-pane label="活动管理" name="first"></el-tab-pane>
        </el-tabs>
        <div class="container mt-1">
          <el-form inline size="small">

            <el-form-item label="活动搜索：">
              <el-input v-model="tableFrom.name" placeholder="请输入活动名称" class="selWidth" size="small" clearable>
                <el-button slot="append" icon="el-icon-search" @click="seachList" size="small"
                           v-hasPermi="['admin:course:list']"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button size="small" type="primary" @click="addActivity" class="mr10">添加活动
        </el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        :highlight-current-row="true"
        :header-cell-style=" {fontWeight:'bold'}"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="50"
        />
        <el-table-column label="活动名称"
                         prop="name"
                         min-width="150"
                         :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="签到二维码" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 50px; height: 50px"
                :src="scope.row.codeString"
                :preview-src-list="[scope.row.codeString]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="关联商品" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.image"
                :preview-src-list="[scope.row.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="codeValue"
          label="签到获得积分"
          min-width="90"
          align="center"
        />
        <el-table-column
          prop="distance"
          label="签到范围（米）"
          min-width="70"
          align="center"
        />
        <el-table-column
          label="关联商品"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime | dataFormat }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" class="mr10" @click="activityLogClick(scope.rows)">签到记录</el-button>
            <el-button type="text" size="small" @click="activityLocationClick(scope.row)" class="mr10">活动点位</el-button>
            <el-button type="text" size="small" class="mr10" @click="activityEditClick(scope.row)">编辑
            </el-button>
            <el-button type="text" size="small" class="mr10" @click="deleteActivityClick(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-dialog  :visible.sync="activityAddVisible" title="新增活动" center width="60%" append-to-body>
      <el-form ref="formValidate" v-loading="fullscreenLoading" class="formValidate mt20" :rules="ruleValidate"
               :model="formValidate" label-width="150px"
      >
        <el-row :gutter="24">
          <el-col v-bind="grid2">
            <el-form-item label="活动名称：" prop="name">
              <el-input v-model="formValidate.name" maxlength="249" placeholder="请输入课程名称" :disabled="isDisabled"/>
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="签到积分：" prop="codeValue">
              <el-input-number v-model="formValidate.codeValue" :min="1" :max="100" :disabled="isDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col v-bind="grid2">
            <el-form-item label="签到范围（米）：" prop="distance">
              <el-input-number v-model="formValidate.distance" :min="1000" :max="1000000" :step="500"
                               :disabled="isDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="关联商品：" prop="image">
              <div class="upLoadPicBox" @click="changeGood">
                <div v-if="formValidate.image" class="pictrue"><img :src="formValidate.image"></div>
                <div v-else class="upLoad">
                  <i class="el-icon-camera cameraIconfont"/>
                </div>
              </div>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('formValidate') ">取 消</el-button>
            <el-button type="primary" @click="saveActivity">确 定</el-button>
          </span>
    </el-dialog>

    <el-dialog v-if="activityLocationVisible" :visible.sync="activityLocationVisible" title="活动点位" center width="800px">
      <activity-location :activity-id="activityId"></activity-location>
      <div slot="footer" class="dialog-footer">
        <el-button @click="activityLocationVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <el-dialog v-if="activityLogVisible" :visible.sync="activityLogVisible" title="签到记录" center width="900px">
      <activity-log :activity-id="activityId"></activity-log>
      <div slot="footer" class="dialog-footer">
        <el-button @click="activityLogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  productLstApi,
  productDeleteApi,
  categoryApi,
  putOnShellApi,
  offShellApi,
  productHeadersApi,
  productExportApi,
  restoreApi,
  productExcelApi
} from '@/api/store'
import { courseLstApi, addCourseApi, courseDeleteApi, onlineOffApi, recycleCourseApi } from '@/api/course'
import { activityLstApi, addActivityApi, deleteActivityApi,updateActivityApi } from '@/api/activity'
import { getToken } from '@/utils/auth'
import { checkPermi } from '@/utils/permission' // 权限判断函数

import activityLocation from './activityLocation/activityLocation'
import activityLog from '@/views/activity/activityLog'
const activityForm = {
  name: '',
  type: 1,
  codeValue: 10,
  distance: 2000,
  id: 0,
  image: '',
  productId: 0
}
export default {
  name: 'ActivityList',
  components: { activityLocation ,activityLog},
  data() {
    return {
      activeName: 'first',
      activityAddVisible: false,
      activityLocationVisible: false,
      activityLogVisible:false,
      props: {
        children: 'child',
        label: 'name',
        value: 'id',
        emitPath: false,
        checkStrictly: true
      },
      // roterPre: roterPre,
      headeNum: [{ name: '课程', type: '1' }, { name: '课程回收站', type: '5' }],
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        name: null
      },
      categoryList: [],
      merCateList: [],
      objectUrl: process.env.VUE_APP_BASE_API,
      fullscreenLoading: false,
      ruleValidate: {
        name: [
          { required: true, message: '请输入课程名称', trigger: 'blur' }
        ],
        cateIds: [
          { required: true, message: '请选择课程分类', trigger: 'change', type: 'array', min: '1' }
        ],
        description: [
          { required: true, message: '课程简介不能为空', trigger: 'blur' }
        ],
        subName: [
          { required: true, message: '副标题不能为空', trigger: 'blur' }
        ]

      },

      formValidate: Object.assign({}, activityForm),
      grid2: {
        xl: 12,
        lg: 12,
        md: 12,
        sm: 24,
        xs: 24
      },
      grid3: {
        xl: 24,
        lg: 24,
        md: 24,
        sm: 24,
        xs: 24
      },
      courseLabelList: [],
      tagDialog: {
        visible: false,
        form: {
          label: '',
          labelType: ''
        },
        rule: {
          label: [
            { required: true, message: '请输入标签名称', trigger: 'blur' }
          ],
          labelType: [
            { required: true, message: '请选择标签类型', trigger: 'change' }
          ]
        }
      },
      isDisabled: false,
      props2: {
        children: 'child',
        label: 'name',
        value: 'id',
        emitPath: false,
        checkStrictly: true
      },
      productId: 0,
      activityId: 0,
      editActivity: false
    }
  },
  mounted() {
    this.getList()
    this.getCategorySelect()
    this.checkedCities = this.$cache.local.has('goods_stroge') ? this.$cache.local.getJSON('goods_stroge') : this.checkedCities
  },
  methods: {

    checkPermi,
    // 点击商品图
    modalPicTap(tit, num, i, status) {
      const _this = this
      if (_this.isDisabled) return
      this.$modalUpload(function(img) {
        if (tit === '1' && !num) {
          _this.formValidate.image = img[0].sattDir
          _this.formValidate.imageAttId = img[0].attId
        }

      }, tit, 'content')
    },
    addActivity() {
      this.editActivity = false
      if (this.activityAddVisible == true) {
        this.activityAddVisible = false
      } else {
        this.activityAddVisible = true
      }
    },
    saveActivity() {
      this.listLoading = true
      if (this.editActivity == true) {
        updateActivityApi(this.formValidate).then(res=>{
          console.info(res)
          this.listLoading = false
          this.resetForm('formValidate')
          this.getList()
        }).catch(res=>{
          this.listLoading = false
          this.$message.error(res.message)
        })
      } else {
        addActivityApi(this.formValidate).then(res => {
          console.info(res)
          this.listLoading = false
          this.resetForm('formValidate')
          this.getList()
        }).catch(res => {
          this.listLoading = false
          this.$message.error(res.message)
        })
      }

    },
    handleRestore(id) {
      this.$modalSure('恢复课程').then(() => {
        recycleCourseApi({ id: id, isRecycle: false })
          .then((res) => {
            this.$message.success('操作成功')
            this.getList()
          })
      })
    },
    seachList() {
      this.tableFrom.page = 1
      this.getList()
    },
    resetForm(formName) {
      this.activityAddVisible = false
      this.$refs[formName].resetFields()
    },
    // 导出
    exports() {
      productExcelApi({
        cateId: this.tableFrom.cateId,
        name: this.tableFrom.name
      }).then((res) => {
        window.location.href = res.fileName
      })
    },
    // 课程分类；
    getCategorySelect() {
      categoryApi({ status: -1, type: 8 }).then(res => {
        this.merCateList = res
      }).catch(res => {
        this.$message.error(res.message)
      })
    },
    // 列表
    getList() {
      this.listLoading = true
      activityLstApi(this.tableFrom).then(res => {
        this.tableData.data = res.list
        this.tableData.total = res.total
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    // 删除
    handleDelete(id, type) {
      this.$modalSure(`删除 id 为 ${id} 的课程`).then(() => {
        const deleteFlag = type == 5 ? 'delete' : 'recycle'
        courseDeleteApi(id, deleteFlag).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    activityLocationClick(row) {
      this.activityLocationVisible = true
      this.activityId = row.id
    },
    activityLogClick(row) {
      this.activityLogVisible = true
      this.activityId = row.id
    },
    changeGood() {
      const _this = this
      this.$modalGoodList(function(row) {
        _this.formValidate.image = row.image
        _this.productId = row.id
        _this.formValidate.productId = row.id
        // _this.formValidate.productId = row.id
      })
    },
    activityEditClick(row) {
      if (this.activityAddVisible == true) {
        this.activityAddVisible = false
      } else {
        this.activityAddVisible = true
      }
      this.formValidate.id = row.id
      this.formValidate.codeValue = row.codeValue
      this.formValidate.image = row.image
      this.formValidate.productId = row.productId
      this.formValidate.distance = row.distance
      this.formValidate.name = row.name
      this.editActivity = true
    },
    deleteActivityClick(row) {
      this.$modalSure(`删除 id 为 ${row.id} 的活动`).then(() => {
        this.listLoading = true
        deleteActivityApi({ id: row.id }).then(res => {
          console.info(res)
          this.listLoading = false
          this.getList()
        }).catch(res => {
          this.listLoading = false
          this.$message.error(res.message)
        })
      })

    }

  }
}
</script>

<style scoped lang="scss">
.el-table__body {
  width: 100%;
  table-layout: fixed !important;
}

.taoBaoModal {
  //  z-index: 3333 !important;
}

.demo-table-expand {
  ::v-deep label {
    width: 82px;
  }
}

.demo-table-expand {
  ::v-deep .el-form-item__content {
    width: 77%;
  }
}

.selWidth {
  width: 350px !important;
}

.seachTiele {
  line-height: 30px;
}

.relative {
  position: relative;
}
</style>
